// 详情页接口

import Mock from "mockjs"
import {param2Obj} from "../utils/urlUtils"

let detailList = [] // 所有数据
Temper()

// ecart 温度数据
let temperature = [];
for(let i = 0;i<12;i++){
    temperature.push({
        OutdoorTemperature:Mock.mock('@integer(15,38)'),
        OutdoorHumidity:Mock.mock('@integer(15,38)'),
    })
}
// 每周数据
let weekTem = []
for(let i = 0;i<7;i++){
    weekTem.push({
        OutdoorTemperature:Mock.mock('@integer(15,38)'),
        OutdoorHumidity:Mock.mock('@integer(15,38)'),
    })
}
// 本月数据
let monthTem = []
for(let i=0;i<30;i++){
    monthTem.push({
        OutdoorTemperature:Mock.mock('@integer(25,35)'),
        OutdoorHumidity:Mock.mock('@integer(25,35)'),
    })
}
//数据抛出
export default {
    getOutdoorTemp: config => {
        var { keyword } = JSON.parse(config.body);
        var res =  detailList.filter(item=>item.date.indexOf(keyword) != -1)
        // console.log(config,res);
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    getTemperature:()=>{
        return {
            code: 200,
            msg: '请求成功',
            data: temperature
        }
    },
    getWeekTem:()=>{
        return {
            code: 200,
            msg: '请求成功',
            data: weekTem
        }
    },
    getMonthTem:()=>{
        return {
            code: 200,
            msg: '请求成功',
            data: monthTem
        }
    }
    
}

//历史数据函数
function Temper() {
    var time = Mock.mock("@now('hour')").split(' ')
    var time0 = time[0]
    var time1 = time[1]
    var time0 = time0.split('-')
    var time1 = time[1]
    var time1 = time1.split(':')
    for (var i = 0; i < 7; i++) {
        let a = new Date(time0[0], time0[1], time0[2] - i)
        let year = a.getFullYear()
        let month = a.getMonth()
        let day = a.getDate()
        let average = 0
        let b = []
        if (i == 0) {
            average = b.reduce((prev, item) => prev + item, 0) / time1[0]
        } else {
            average = b.reduce((prev, item) => prev + item, 0) / 24
        }
        // indoorTemperature[`${year}${month}${day}`] = b
        // average = Math.round(average)
        detailList.push({
            date: `${year}-${month}-${day}`,
            id:Mock.mock('@guid()'), 
            indoorTemperature:Mock.mock('@integer(15,39)') +'°C',  // 室外温度
            OutdoorTemperature:Mock.mock('@integer(15,39)') +'°C',
            indoorHumidity:Mock.mock('@integer(15,39)') +'°C',  // 室外湿度
            OutdoorHumidity:Mock.mock('@integer(15,39)') +'°C',
            LightIntensity:Mock.mock('@integer(30,45)') +'lux', // 光照强度
            nitrogen:Mock.mock('@integer(200,300)'), // 氮
            phosphorus:Mock.mock('@integer(60,100)'), // 磷
            potassium:Mock.mock('@integer(90,200)'), // 钾
            CO2:Mock.mock('@integer(200,400)'), // 氧气
            EC:Mock.mock('@integer(1,9)'), 
            PH:Mock.mock('@integer(5,9)'),
        })
    }
}

